package ccnt.pvc.scudwaremobile.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import ccnt.pvc.scudwaremobile.R;
import ccnt.pvc.scudwaremobile.bean.ResultObject;
import ccnt.pvc.scudwaremobile.dao.ServiceModel;
import ccnt.pvc.scudwaremobile.storage.ServiceDbManager;
import ccnt.pvc.scudwaremobile.utils.HttpClientUtils;
import ccnt.pvc.scudwaremobile.utils.ThreadPoolUtils;

public class CoopServiceDetailActivity extends Activity {

	private final static String TAG = CoopServiceDetailActivity.class.getName();
	private final static int REQUEST_SUCCESS = 1;
	
	ServiceDbManager serviceDbManager;
	ServiceModel serviceModel;
	String className;
	boolean isReg;
	String account;
	String ip;
	
	String requestResult;
	
	TextView textServiceDetail;
	Button btnInvoke;
	TextView txtResult;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coop_servicedetail);
		
		Bundle bundle = getIntent().getExtras();
		className = bundle.getString("className");
		isReg = bundle.getBoolean("isReg");
		account = bundle.getString("account");
		ip = bundle.getString("ip");
		
		String json = bundle.getString("serviceModel");
		serviceModel = new Gson().fromJson(json, ServiceModel.class);
		
		initService();
		initView();
	}
	
	private void initService() {
		serviceDbManager = new ServiceDbManager(CoopServiceDetailActivity.this);
	}
	
	private void initView() {
		textServiceDetail = (TextView)findViewById(R.id.textServiceDetail);
		
//		if(isReg) {
//			serviceModel = serviceDbManager.getRegServiceByClassName(className);
//		} else {
//			serviceModel = serviceDbManager.getServiceByClassName(className);
//		}
		
		StringBuilder builder = new StringBuilder();
		builder.append("类名：" + serviceModel.getClassName() + "\n");
		builder.append("包名：" + serviceModel.getPackageName() + "\n");
		builder.append("所属设备：" + serviceModel.getDeviceName() + "\n");
		builder.append("服务类型：" + (serviceModel.getCategory() == 1 ? "数据服务" : "应用服务") + "\n");
		builder.append("传感器：" + serviceModel.getSensorName() + "\n");
		builder.append("数据分类：" + serviceModel.getDataType() + "\n");
		builder.append("度量：" + serviceModel.getMeasure() + "\n");
		builder.append("服务类型：" + (serviceModel.getLevel() == 0 ? "raw" : "inferred") + "\n");
		builder.append("参数：" + serviceModel.getParams() + "\n");
		builder.append("描述：" + serviceModel.getDescription() + "\n");
		textServiceDetail.setText(builder.toString());
		
		txtResult = (TextView)findViewById(R.id.txtResult);
		
		btnInvoke = (Button)findViewById(R.id.btnInvoke);
		btnInvoke.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				StringBuilder builder = new StringBuilder();
//				builder.append("来自" + account + ":" + ip + "的结果:" + "\n");
//				builder.append("{" + "\n");
//				builder.append("\"icon-small\": 17302824," + "\n");
//				builder.append("\"present\": true," + "\n");
//				builder.append("\"scale\": 100," + "\n");
//				builder.append("\"level\": 39," + "\n");
//				builder.append("\"technology\": \"Li-poly\"," + "\n");
//				builder.append("\"status\": 2," + "\n");
//				builder.append("\"voltage\": 3942," + "\n");
//				builder.append("\"invalid_charger\": 0," + "\n");
//				builder.append("\"plugged\": 2," + "\n");
//				builder.append("\"health\": 2," + "\n");
//				builder.append("}" + "\n");
//				txtResult.setText(builder.toString());
				ThreadPoolUtils.execute(new Runnable() {
					
					@Override
					public void run() {
						remoteRequest();
					}
				});
			}
		});
	}
	
	private void remoteRequest() {
		Map<String, Object> params = new HashMap<String, Object>();
		ResultObject resultObject = HttpClientUtils.sendGet("http://" + ip + ":8080/service/" + className + "/data.json", params);
		int code = resultObject.getCode();
		if(code == 200) {
			String result = resultObject.getResult();
			Log.d(TAG, resultObject.getResult());
			requestResult = result;
			handler.sendMessage(handler.obtainMessage(REQUEST_SUCCESS));
		}
	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case REQUEST_SUCCESS:
				txtResult.setText(requestResult);
				break;
			default:
				super.handleMessage(msg);
			}
			
		}
		
	};
}

